package com.mgg.payment.service.order.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.mgg.common.dao.DaoSupport;
import com.mgg.common.dto.OrderDTO;
import com.mgg.common.enums.OrderStatusEnum;
import com.mgg.payment.service.account.AccountService;
import com.mgg.payment.service.order.SalesOrderService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lijun
 * 
 * @version
 */

@Service("orderService")
public class SalesOrderServiceImpl implements SalesOrderService {

	@Resource(name = "daoSupport")
	private DaoSupport dao;

	@Autowired
    private AccountService accountService;
	
	/**
	 * 根据订单ID查找订单
	 */
	@Override
	public OrderDTO getOrderByOrderId(String orderId) {
		OrderDTO order = dao.findForObject("SalesOrderMapper.getOrderByOrderId", orderId);
		return order;
	}

	/**
	 * 修改订单
	 */
	@Override
	public Integer updateOrder(String orderId, String payWay, String tradeNo, String payTime) {
		Map<String,Object> map = new HashMap<>();
		map.put("orderId", orderId);
		map.put("payWay", payWay);
		map.put("tradeNo", tradeNo);
		map.put("payTime", payTime);
		map.put("status", OrderStatusEnum.PAID.getCode());
		Integer count = dao.update("SalesOrderMapper.updateOrder", map);
		return count;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> listOrderDetailGoodsRebateInfo(String orderId) {
		return  dao.findForList("SalesOrderMapper.listOrderDetailGoodsRebateInfo", orderId);
	}

	@Override
	public Map<String, Integer> getOpenGiftConfigByOrderId(String orderId) {
		String goodsType = dao.findForObject("SalesOrderMapper.getOpenGiftGoodsTypeByOrderId", orderId);
		Map<String,Integer> resultMap = dao.findForObject("SalesOrderMapper.getOpenGiftConfigByOrderId", orderId);
		if (MapUtils.isEmpty(resultMap)) {			
			resultMap = new HashMap<>();
			if ("3".equals(goodsType)) {
				resultMap.put("order_type", 5);
				resultMap.put("give_num", 0);
			} else {
				resultMap.put("order_type", 7);
				resultMap.put("give_num", 0);
			}
		}
		return resultMap;
	}
}
